package com.wlyuan.open.domain.source.assembler;

import com.wlyuan.open.domain.source.utils.MappingOrder;
import com.wlyuan.open.dto.OpenUserSyncDTO;
import com.wlyuan.user.dto.UserRewriteDTO;
import lombok.var;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * @author yuanzheng
 */
public class UserAssembler {

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    @Mapper(uses = MappingOrder.class)
    public interface Assembler {
        @Mappings({
                @Mapping(target = "userNick", source = "nick"),
                @Mapping(target = "avatar", source = "portrait"),
                @Mapping(target = "nick", source = "nickName"),
                @Mapping(target = "appId", constant = ""),
                @Mapping(target = "userId", source = "sourceKey"),
        })
        UserRewriteDTO assemble(OpenUserSyncDTO source);
    }

    public static UserRewriteDTO assemble(OpenUserSyncDTO source) {
        var creation = ASSEMBLER.assemble(source);
        creation.setPassword(source.getPassword());
        creation.setAccount(source.getAccount());
        creation.setUserNick(source.getNick());
        creation.setUserId(Long.valueOf(source.getSourceKey()));
        return creation;
    }
}
